package com.alpaca.recommend.bean;

import com.alpaca.recommend.hbase.activerecord.HModel;
import com.alpaca.recommend.util.MD5Util;

import java.io.IOException;

/**
 * Created by wangj on 2015/2/10.
 */
public class UItem extends HModel<UItem> {
    public static final UItem dao = new UItem();

    public static final String TABLE_NAME = "u_items";
    public static final String U_ITEM_FAMILY = "i";
    public static final String U_ITEM_RANK_COLUMN = "r";
    public static final String U_ITEM_IS_READ_COLUMN = "ir";

    public static final Integer RANK_MAX = 99;

    public static String getRowKey(String channelCode, String userId, String itemId){
        return  MD5Util.md5(String.valueOf(userId)) + MD5Util.md5(String.valueOf(channelCode)) + itemId;
    }

    public void init(String channelCode, String userId, String itemId, Integer rank, String isRead) {
        //设置rowkey.
        String rowkey = MD5Util.md5(String.valueOf(userId)) + MD5Util.md5(String.valueOf(channelCode)) + itemId;

        setRow(rowkey);
        //设置分数.
        set(U_ITEM_FAMILY, U_ITEM_RANK_COLUMN, rank);
        //设置是否可读.
        set(U_ITEM_FAMILY, U_ITEM_IS_READ_COLUMN, isRead);
    }

    public void init(String channelCode, String userId, String itemId, Integer isRead) {
        //设置rowkey.
        String rowkey = MD5Util.md5(userId) + MD5Util.md5(channelCode) + itemId;

        setRow(rowkey);
        //设置是否可读.
        set(U_ITEM_FAMILY, U_ITEM_IS_READ_COLUMN, isRead);
    }


    public static void main(String[] args) throws IOException {
//        HTableInterface tagItemsTable=new HTableClients().getHTable(UItem.TABLE_NAME);
//        Scan scan = new Scan();
//        ResultScanner rs = tagItemsTable.getScanner(scan);
//        for(Result result:rs){
//            Cell cell = result.getColumnLatestCell(UItem.U_ITEM_FAMILY,UItem.U_ITEM_RANK_COLUMN);
//            System.out.println(Bytes.toInt(CellUtil.cloneValue(cell)));
//        }
    }
}
